package problemLCR019;

//LCR 019. 验证回文串 II
//https://leetcode.cn/problems/RQku0D/
class Solution {
    public boolean validPalindrome(String s) {
        int count = 0;
        int i = 0, j = s.length()-1;
        while(i < j) {
            if(s.charAt(i) != s.charAt(j)) {
                if(++count > 1) return false;
                if(check(s, i+1, j) || check(s, i, j-1)) return true;
            }else {
                i++;
                j--;
            }
        }
        return true;
    }

    private boolean check(String s, int i, int j) {
        while(i < j) {
            if(s.charAt(i++) != s.charAt(j--)) {
                return false;
            }
        }
        return true;
    }
}

